/*
 * Copyright 2008 Andrej Herich
 * 
 * http://azetclient.yw.sk/
 * http://azetclient.googlecode.com/
 * 
 * This file is part of AzetClient.
 * 
 * AzetClient is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * AzetClient is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with AzetClient.  If not, see <http://www.gnu.org/licenses/>.
 */

package sk.yw.azetclient.gui;

import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.List;
import org.apache.log4j.Logger;

/**
 *
 * @author error216
 */
public class ConversationFocusQueue {
    
    private static final Logger logger =
            Logger.getLogger(ConversationFocusQueue.class);
    
    private ArrayDeque<ConversationPanel> panels =
            new ArrayDeque<ConversationPanel>();
    
    private List<ConversationFocusQueueListener> listeners
            = new ArrayList<ConversationFocusQueueListener>();
    
    public void addConversationFocusQueueListener(
            ConversationFocusQueueListener listener) {
        listeners.add(listener);
    }
    
    public void removeConversationFocusQueueListener(
            ConversationFocusQueueListener listener) {
        listeners.remove(listener);
    }
    
    protected void fireQueueIsEmpty() {
        for (ConversationFocusQueueListener listener : listeners) {
            listener.queueIsEmpty();
        }
    }
    
    protected void fireQueueIsNotEmpty() {
        for (ConversationFocusQueueListener listener : listeners) {
            listener.queueIsNotEmpty();
        }
    }
    
    public void add(ConversationPanel panel) {
        synchronized (panels) {
            panels.add(panel);
            if (panels.size() == 1) fireQueueIsNotEmpty();
        }
        logger.debug("Component added to focus queue");
    }
    
    public void remove(ConversationPanel panel) {
        synchronized (panels) {
            panels.remove(panel);
            if (panels.isEmpty()) fireQueueIsEmpty();
        }
        logger.debug("Component removed from focus queue");
    }
    
    public void requestNextFocus() {
        synchronized (panels) {
            ConversationPanel firstPanel = panels.peek();
            while (panels.peek() != null
                    && !panels.peek().equals(firstPanel))
                panels.add(panels.poll());
            
            if (panels.peek() != null) {
                panels.peek().getFrame()
                        .setSelectedConversationPanel(panels.peek());
                panels.peek().requestFocusOnInputTextPane();
            }
        }
        logger.debug("Next focus requested");
    }
    
    public boolean isEmpty() {
        synchronized (panels) {
            return panels.isEmpty();
        }
    }

}
